﻿namespace HousholdISv2
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using GalaSoft.MvvmLight.Messaging;

    /// <summary>
    /// Interaction logic for CustomDialog.xaml
    /// </summary>
    public partial class CustomDialog : Window
    {
        private bool isClosing;

        public CustomDialog()
        {
            InitializeComponent();
            Messenger.Register<NotificationMessage>(this, this.ProcessMessage);
        }

        private Messenger Messenger
        {
            get { return Messenger.Default; }
        }

        private void ProcessMessage(NotificationMessage message)
        {
            if (this.Content != message.Sender)
            {
                return;
            }

            if (!message.Notification.Equals("Close", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            if (!this.isClosing)
            {
                this.Close();
            }
        }

        private void WindowClosing(object sender, CancelEventArgs e)
        {
            this.isClosing = true;
            Messenger.Unregister<NotificationMessage>(this, this.ProcessMessage);
        }
    }
}
